/**
 * Copyright 2009 Sergio Bossa
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.googlecode.actorom.dsl.chain.messaging;

import com.googlecode.actorom.Actor;
import com.googlecode.actorom.Address;
import com.googlecode.actorom.Topology;
import com.googlecode.actorom.support.ActorInvocationException;
import net.jcip.annotations.ThreadSafe;

/**
 * @author Sergio Bossa
 */
@ThreadSafe
public final class UnlinkMethodExecutor {

    private final Topology topology;
    private final Address sourceAddress;

    public UnlinkMethodExecutor(Topology topology, Address sourceAddress) {
        this.topology = topology;
        this.sourceAddress = sourceAddress;
    }

    /**
     * Set the actor {@link com.googlecode.actorom.Address} to unlink from.
     */
    public void from(Address fromAddress) {
        Actor sourceActor = topology.getActor(sourceAddress);
        Actor fromActor = topology.getActor(fromAddress);
        if (sourceActor != null && fromActor != null) {
            sourceActor.unlink(fromActor);
        } else if (sourceActor == null) {
            throw new ActorInvocationException("No actor found at address: " + sourceAddress);
        } else {
            throw new ActorInvocationException("No actor found at address: " + fromAddress);
        }
    }
}
